#include "../include/threadPool.h"

void *threadFunc(void *arg){
    threadPool_t * pthreadPool = (threadPool_t *)arg;
    while(1){
        int netfd;
		int command;
		char *data;
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize == 0){
            pthread_cond_wait(&pthreadPool->cond, &pthreadPool->mutex);
        }
        
        netfd = pthreadPool->taskQueue.pFront->netfd;
		command = pthreadPool->taskQueue.pFront->command;
		data = pthreadPool->taskQueue.pFront->data;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);

        printf("I am worker! I got netfd = %d\n,command = %d\n", netfd,command);
		char *username;
		char *passwd;
		switch(command) {
            case CMD_USER:          //  增加一项	
				username = data;
				//int ret = getEncryptedPasswd(username,&encrypted_passwd);
				//ERROR_CHECK(ret,-1,"getEncryptedPasswd");
                printf(" %3s",username);
				printf("请输入密码：\n");
			case CMD_AUTH:         // 认证
				//接收账号密码，用用户名得到暗文，对密码加盐哈希，比较放入
				//如果一致就显示登陆成功，把该netfd和用户名，放入用户数组，
				//否就叫他重输
				//char encrypted_passwd[256];
				//char salt_hash[256];
				//bzero(&real_passwd,sizeof(real_passwd));
				//bzero(&salt_hash,sizeof(salt_hash));

				//int ret = getSaltHash(passwd,&salt_hash);
				//ERROR_CHECK(ret,-1,"getSaltHash");
					
				//if(strcmp(encrypted_passwd,salt_hash) == 0){
				//	printf("密码正确，登陆成功！\n")
				//	fdtoname[netfd] = username; 
				//}else{
				//	printf("用户名或密码错误！\n");
				//}
                printf("请进行身份验证\n");
				break;				
			case CMD_ECHO:         // 回显测试

				break;
			case CMD_LS_REQ:       // 列出文件
				printf("您还没有完成登录!\n");
				break;
			case CMD_LS_RESP:
				
				break;
			case CMD_CD_REQ:       // 改变目录
 
				break;
			case CMD_CD_RESP:

				break;
			case CMD_PWD_REQ:      // 显示当前目录

				break;
			case CMD_PWD_RESP:

				break;
			case CMD_RMOVE_REQ:    // 删除文件

				break;
			case CMD_RMOVE_RESP:

				break;
			case CMD_MKDIR_REQ:    // 创建目录

				break;
			case CMD_MKDIR_RESP:

				break;
			case CMD_RMDIR_REQ:    // 删除目录

				break;
			case CMD_RMDIR_RESP:

				break;
			case CMD_GETS_REQ:     // 下载文件
				transfile(netfd);
				break;
			case CMD_GETS_RESP:
 
				break;
			case CMD_PUTS_REQ:     // 上传文件

				break;
			case CMD_PUTS_RESP:

				break;
			case CMD_RESPONSE:     // 服务器响应

				break;
			default:

				break;
        close(netfd);
        }
    }
}

int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum; ++i){
        pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,pthreadPool);
    }
    return 0;
}
